<?php
/**
 * Core_Model_Xssprotection
 * xss protection model
 * @author Adrian Stolarski
 */
class Core_Model_Xssprotection implements Core_Model_Xssprotectable, Core_Model_Decidable {
    /**
     *
     * @var type $xss_filter
     * xss filter handler
     */
    public $xss_filter;
    /**
     * xss_entrypoint()
     * entry point to parse variables array
     * @param array $variables
     * @param type $action
     * @return array $variables
     */
    public function xss_entrypoint(array $variables = null, $action = null) {
        foreach($variables as $key => $value){
            if(is_array($value)){
                $variables[$key] = $this->xss_entrypoint($value, $action);
            }
            else {
                $variables[$key] = $this->decision($value, $action);
            }
        }
        return $variables;
    }
    
    /**
     * decision()
     * xss filter decision
     * @param type $value
     * @param type $action
     * @return type $value
     */
    public function decision($value = null, $action = null) {
        switch($action){
            case "basic_xss_filter":
                $value = $this->basic_xss_filter($value);
                break;
            case "fix_entity_filter":
                $value = $this->xss_fix_entity_filter($value);
                break;
            case "xmlns_filter":
                $value = $this->xmlns_filter($value);
                break;
            case "jsandvb":
                $value = $this->remove_java_scripts_and_visual_basic_scripts_protocols($value);
                break;
            case "xss_internet_explorer":
                $value = $this->xss_internet_explorer($value);
                break;
            case "xss_namaspaced_elements":
                $value = $this->xss_namaspaced_elements($value);
                break;
            case "all_xss_filters":
                $value = $this->all_xss_filters($value);
                break;
            default: 
                $value = $this->all_xss_filters($value);
                break;
        }
        return $value;
    }
    
    /**
     * basic_xss_filter()
     * basic xss filter
     * @param type $value
     * @return type $value
     */
    public function basic_xss_filter($value){
        $this->xss_filter = new Core_Data_Basicxssfilter;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /** 
     * xss_fix_entity_filter()
     * xss fix entity filter
     * @param type $value
     * @return type $value
     */
    public function xss_fix_entity_filter($value){
        $this->xss_filter = new Core_Data_Fixentity;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /**
     * xmlns_filter()
     * xmlns filter
     * @param type $value
     * @return type $value
     */
    public function xmlns_filter($value){
        $this->xss_filter = new Core_Data_Xmlns;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /**
     * remove_java_scripts_and_visual_basic_scripts_protocols()
     * 
     * @param type $value
     * @return string $value;
     */
    public function remove_java_scripts_and_visual_basic_scripts_protocols($value){
        $this->xss_filter = new Core_Data_Xssscripts;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /**
     *  xss_internet_explorer()
     * Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
     * @param type $value
     * @return type $value
     */
    public function xss_internet_explorer($value){
        $this->xss_filter = new Core_Data_Xssinternetexplorer;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /**
     * xss_namespaced_elements()
     * Remove namespaced elements (we do not need them)
     * 
     * @param type $value
     * @return type $value
     */
    public function xss_namaspaced_elements($value){
        $this->xss_filter = new Core_Data_Removenamespacedelements;
        $value = $this->xss_filter->xss_filter($value);
        return $value;
    }
    
    /**
     * all_xss_filters()
     * all xss filters
     * @param type $value
     * @return type $value
     */
    public function all_xss_filters($value){
        $value = $this->basic_xss_filter($value);
        $value = $this->remove_java_scripts_and_visual_basic_scripts_protocols($value);
        $value = $this->xmlns_filter($value);
        $value = $this->xss_fix_entity_filter($value);
        $value = $this->xss_internet_explorer($value);
        $value = $this->xss_namaspaced_elements($value);
        return $value;
    }

}
